package projet.ejb;

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import projet.entity.Club;
import projet.entity.Compte;
import projet.entity.Investisseur;

@Stateless
public class ClubFacadeEJB implements ClubFacade {
	@PersistenceContext(unitName = "SampleUnit")
	protected EntityManager entityMgr;
	
	public Club add(Club c) {
		entityMgr.persist(c);
		return c;
	}
	
	public Club findByNom(String nom){		
		Query query = entityMgr.createQuery("SELECT c FROM  Club as c WHERE c.nom=:n ");
		query.setParameter("n",nom);
		Club c =   (Club) query.getSingleResult();
		return c;
	}	
	
	@SuppressWarnings("unchecked")
	public Collection<Club> findAll() {
		 return entityMgr.createQuery("SELECT c FROM Club as c").getResultList();
	}
	
	public void deleteAll() {
		Collection<Club> l_club = findAll();
		for(Club c : l_club){
			entityMgr.remove(c);
		}
	}
	
	public Club updateNom(int id ,String nom){
		Club c= entityMgr.find(Club.class, id);
		c.setNom(nom);
		return c;
	}
	
	public void delete(int id) {
		Club c = entityMgr.find(Club.class,id);
			entityMgr.remove(c);
		}
}
